<?php

namespace app\api\service;

use app\api\model\VideoList;

class VideoService
{
    public static function getRecommendList($params)
    {
        $list = VideoList::getRecommendList();
        $data = array_map(function ($item) {
            return [
                'video_id' => $item['video_id'],
                'video_thumb' => dealImage($item['video_thumb']),
                'video_title' => $item['video_title'],
                'level_title' => $item['level_title'],
            ];
        }, $list);
        return [200, '操作成功', $data];
    }

    public static function getVideoList($params)
    {
        $where = [];
        $where[] = ['a.video_status', '=', 1];
        if ($params['keyword']) $where[] = ['a.video_title', 'like', '%' . $params['keyword'] . '%'];
        $list = VideoList::getListByWhere($where, $params['page_no'], $params['page_size']);
        $total = VideoList::countListByWhere($where);
        $data = array_map(function ($item) {
            return [
                'video_id' => $item['video_id'],
                'video_thumb' => dealImage($item['video_thumb']),
                'video_title' => $item['video_title'],
                'level_title' => $item['level_title'],
            ];
        }, $list);
        return [200, '操作成功', ['total' => $total, 'list' => $data]];
    }

    public static function getVideoInfo($params)
    {
        $info = VideoList::getInfoById($params['video_id']);
        if (!$info) return [201, '数据不存在', []];
        $info->save(['video_views' => $info['video_views'] + 1]);
        $data = [
            'video_id' => $info['video_id'],
            'video_thumb' => dealImage($info['video_thumb']),
            'video_url' => dealImage($info['video_url']),
            'video_title' => $info['video_title'],
            'level_title' => $info['level_title'],
        ];
        return [200, '操作成功', $data];
    }
}